package org.example.trigger.listener;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.example.domain.canal.service.ICanalService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * canal监控消费者
 */
@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = "sst-group-canal-to-rocketmq", topic = "canal-to-rocketmq")
public class CanalCustomer implements RocketMQListener<String> {

    @Resource
    private ICanalService canalService;
    @Resource
    private ObjectMapper objectMapper;
    @Override
    public void onMessage(String s) {
        HashMap<String, Object> map = null;
        try {
            map = objectMapper.readValue(s, HashMap.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        //获取具体的哪个表
        if (!map.isEmpty() && map.get("database").toString().equals("big_market")) {
            if(map.get("table").toString().equals("strategy_award")){
                canalService.deleteStrategyAwardRedisByCanalMqMap(map);
            }
        }
    }
}
